<?php
declare (strict_types = 1);

namespace app\controller\api;

use app\BaseController;
use app\facade\WechatpayFacade;
use app\service\logic\common\order\OrderConsultRefundService;
use app\service\logic\common\order\OrderConsultService;
use app\service\logic\common\order\OrderCourseService;
use app\service\logic\common\order\OrderMultiService;
use app\service\logic\common\trtc\TrtcRoomsService;
use think\facade\Log;
use think\response\Json;

class CallbackController extends BaseController
{
    /**
     * alipay 全部回调
     */
    public function alipay()
    {
        // $params = $this->request->param();
        $params = $_POST;
        Log::debug('Callback_ALiPay_params: ' . var_export($params, true));
        if (isset($params['passback_params']) && !empty($params['passback_params'])) {
            // 'passback_params' => '%7B%22order_type%22%3A1%7D',
            $passbackParams = json_decode(urldecode($params['passback_params']), true);
            if (isset($passbackParams['order_type'])) {
                switch ($passbackParams['order_type']) {
                    case 1:
                        // 50000_order_multi
                        invoke(OrderMultiService::class)->aLiPayCallback($params);
                        break;
                    case 2:
                        // 50200_order_consult
                        invoke(OrderConsultService::class)->aLiPayCallback($params);
                        break;
                    case 3:
                        // 50300_order_course
                        invoke(OrderCourseService::class)->aLiPayCallback($params);
                        break;
                    default:
                        break;
                }
            }
        }
    }

    /**
     * wechatpay全部回调
     */
    public function wechatpay()
    {
        $params = $this->request->param();
        Log::debug('callback_wechatPay_params: ' . var_export($params, true));

        if (isset($params['resource'])) {
            // 数据解密
            $response = WechatpayFacade::v3AppNotify($params);
            // dd($response);
            Log::debug('callback_wechatPay_response: ' . var_export($response, true));
            if (isset($response['attach'])) {
                $attach = json_decode(urldecode($response['attach']), true);
                if (isset($attach['order_type'])) {
                    switch ($attach['order_type']) {
                        case 1:
                            // 50000_order_multi
                            invoke(OrderMultiService::class)->wechatPayCallback($response);
                            break;
                        case 2:
                            // 50200_order_consult
                            invoke(OrderConsultService::class)->wechatPayCallback($response);
                            break;
                        case 3:
                            // 50300_order_course
                            invoke(OrderCourseService::class)->wechatPayCallback($response);
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }

    /**
     * @return Json
     */
    public function wechatConsultRefund()
    {
        // 回调成功退元心币
        $params = $this->request->param();
        Log::debug('callback_wechatConsultRefund_params: ' . var_export($params, true));

        if (isset($params['resource'])) {
            // 数据解密
            $response = WechatpayFacade::v3AppNotify($params);
            Log::debug('callback_wechatConsultRefund_response: ' . var_export($response, true));
            invoke(OrderConsultRefundService::class)->wechatRefundCallback($response);
        }


        return amis(['code' => 'SUCCESS', 'message' => '成功']);
    }

    /**
     * trtc 房间回调
     */
    public function trtcRoom()
    {
        $params = $this->request->param();
        Log::debug('callback_trtcRoom_params: ' . var_export($params, true));

        // $params = array (  'EventGroupId' => 1,  'EventType' => 101,  'CallbackTs' => 1654587748120,  'EventInfo' =>   array (    'RoomId' => 1471859825,    'EventTs' => 1654587736,    'EventMsTs' => 1654587736867,    'UserId' => 'local_user_13',  ),);
        // $params = array (  'EventGroupId' => 1,  'EventType' => 103,  'CallbackTs' => 1654587739102,  'EventInfo' =>   array (    'RoomId' => 1471859825,    'EventTs' => 1654587739,    'EventMsTs' => 1654587739020,    'UserId' => 'local_expert_2',    'Role' => 20,    'TerminalType' => 2,    'UserType' => 3,    'Reason' => 1,  ),);
        // $params = array (  'EventGroupId' => 1,  'EventType' => 104,  'CallbackTs' => 1654587791837,  'EventInfo' =>   array (    'RoomId' => 1471859825,    'EventTs' => 1654587790,    'EventMsTs' => 1654587790938,    'UserId' => 'local_expert_2',    'Role' => 20,    'Reason' => 1,  ),);
        if (1 == $params['EventGroupId']) {
            // 房间事件组
            invoke(TrtcRoomsService::class)->roomEvent($params);
        }

        return amis();
    }
}
